<script setup lang='ts'>
import { ESObjectWithLocation } from "esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin-main";
import { PropertyCompCallbackFuncParamsType } from "./base";
import { XbsjEarthUi } from "../../../../scripts/xbsjEarthUi";
import { inject } from "vue";
import { Message } from "earthsdk-ui";

const xbsjEarthUi = inject('xbsjEarthUi') as XbsjEarthUi

type FunctionValue = string | number | boolean | string[] | number[] | boolean[]

const props = withDefaults(defineProps<{
    property: any
    treeItem?: any
}>(), {
});
const emits = defineEmits<{
    (e: 'callback', params: PropertyCompCallbackFuncParamsType): void;
}>();

const { property, treeItem } = props
// console.log(property);

const { paramsProperty } = property;
const readonly = paramsProperty.readonly
const func = async () => {
    if (property.name === '保存观察视角' && treeItem) {
        const { sceneObject } = treeItem
        if (sceneObject instanceof ESObjectWithLocation) {
            sceneObject.calcFlyToParam()
            Message.success('保存视角成功')
        } else {
            let position: [number, number, number];
            let rotation: [number, number, number];
            const res = await xbsjEarthUi.activeViewer?.getCurrentCameraInfo()
            if (!res) {
                Message.warning('无法获取视角')
                return
            }
            position = res.position
            rotation = res.rotation
            //@ts-ignore
            sceneObject.flyInParam = { position, rotation, flyDuration: 1 }
            Message.success('保存视角成功')
        }
       
    } else {
        property.exec();
    }


}
</script>

<template>
    <div class="readonly_flag" :class="readonly ? 'readonly_none' : ''">
        <button @click="func">{{ property.name }}</button>
    </div>
</template>

<style scoped>
button {
    width: auto;
    height: 32px;
    line-height: 12px;
    background: rgba(28, 28, 29, 0.6);
    border-radius: 4px;
    border: 1px solid #3b3c40;
    cursor: pointer;
    color: rgba(230, 230, 230, 1);
    padding: 10px;
    margin-right: 10px;
}

button:hover {
    border: 1px solid rgba(44, 104, 247, 1);
}

.readonly_flag {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: flex-start;
}


.readonly_none {
    pointer-events: none;
}
</style>
